﻿/*
///    This file is part of BannerLib4Net.
///
///    BannerLib4Net is free software: you can redistribute it and/or modify
///    it under the terms of the GNU General Public License as published by
///    the Free Software Foundation, either version 3 of the License, or
///    (at your option) any later version.

///    BannerLib4Net is distributed in the hope that it will be useful,
///    but WITHOUT ANY WARRANTY; without even the implied warranty of
///    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
///    GNU General Public License for more details.
///
///    You should have received a copy of the GNU General Public License
///    along with BannerLib4Net.  If not, see <http://www.gnu.org/licenses/>.
*/


using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using BannerLib4Net.Core.Utilities.Configuration;

namespace BannerLib4Net.Security.Encryption
{
    internal class TripleDESEncryptor : IEncryptor
    {

        private String encryptionKey = String.Empty;

        internal TripleDESEncryptor()
        {

        }

        private void Initialize()
        {
            if (!SettingsUtility.IsSettingConfigured("3des.encryption.key"))
            {
                throw new Security.Exceptions.SecurityConfigurationException("You must configure the setting:'3des.encryption.key' to use the TripleDESEncryptor");
            }
            else
            {
                encryptionKey = SettingsUtility.ReadStringValue("3des.encryption.key");
            }
        }

        public string Encrypt(string plainText)
        {

            //String EncryptionKey = new AppSettingsReader ().GetValue ("system.key.3des", typeof(String)).ToString ();
            byte[] Results;
            System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();

            // Step 1 -  hash the key using MD5
            // the MD5 hash generator creates a 128 bit byte array
            // which is a valid length for the TripleDES encoder used below
            //MD5 md5 = new MD5CryptoServiceProvider();
            //byte[] key = md5.ComputeHash(UTF8.GetBytes(EncryptionKey));

            // Step 2 - Create a new TripleDESCryptoServiceProvider object
            TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider();

            // Step 3 - Setup the encoder
            TDESAlgorithm.Key = Convert.FromBase64String(encryptionKey);
            TDESAlgorithm.Mode = CipherMode.ECB;
            TDESAlgorithm.Padding = PaddingMode.PKCS7;

            // Step 4 - Convert the input string to a byte[]
            byte[] DataToEncrypt = UTF8.GetBytes(plainText);

            // Step 5 - Attempt to encrypt the st			{
            ICryptoTransform Encryptor = TDESAlgorithm.CreateEncryptor();
            Results = Encryptor.TransformFinalBlock(DataToEncrypt, 0, DataToEncrypt.Length);
            {
                // Clear the TripleDes and Hashprovider services of any sensitive information
                TDESAlgorithm.Clear();
                //HashProvider.Clear();
            }

            // Step 6 - Return encrypted string as base 64
            return Convert.ToBase64String(Results);
        }

        public string Decrypt(string encryptedText)
        {
            byte[] Results;
            System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();

            // Step 1 -  hash the key using MD5
            // the MD5 hash generator creates a 128 bit byte array
            // which is a valid length for the TripleDES encoder used below
            //TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
            //tdes.Key = Convert.FromBase64String("ainduhgtyenainduheyfthnoajuew213");
            //tdes.IV = Convert.FromBase64String("ainduhgtyenainduheyfthnoajuew213");

            // Step 2 - Create a new TripleDESCryptoServiceProvider object
            TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider();
            //TDESAlgorithm.GenerateIV();
            // Step 3 - Setup the decoder
            TDESAlgorithm.Key = Convert.FromBase64String(encryptionKey);
            TDESAlgorithm.Mode = CipherMode.ECB;
            TDESAlgorithm.Padding = PaddingMode.PKCS7;

            // Step 4 - Convert the input string to a byte[]
            byte[] DataToDecrypt = Convert.FromBase64String(encryptedText);

            // Step 5 - Attempt to decrypt the st			{
            ICryptoTransform Decryptor = TDESAlgorithm.CreateDecryptor();
            Results = Decryptor.TransformFinalBlock(DataToDecrypt, 0, DataToDecrypt.Length);
            {
                // Clear the TripleDes and Hashprovider services of any sensitive information
                TDESAlgorithm.Clear();
                //HashProvider.Clear();
            }

            // Step 6 - Return decrypted string as utf8
            return UTF8.GetString(Results);
        }



    }

}
